package movEat.Kernel.Import;

import movEat.Definitions.Decade;
import movEat.Definitions.Genre;
import movEat.Definitions.Rating;

public class Utils {

	/**
	 * This method checks if a given string is valid ascii.
	 * 
	 * @param str
	 * @return True if the given string is valid ascii
	 */
	public static boolean isValidAscii(String str) {
		if (str == null) {
		    return true;
		}
		for (int i=0; i<str.length(); i++) {
		    char c = str.charAt(i);
		    if (c == '"') return false;
		    if (c >= ' ' && c <= '!') continue;
		    if (c >= '#' && c <= '$') continue;	    
		    if (c >= '&' && c <= ')') continue;
		    if (c >= '<' && c <= '>') continue;
		    if (c >= ',' && c <= '?') continue;
		    if (c >= 'A' && c <= 'Z') continue;
		    if (c >= 'a' && c <= 'z') continue;
		    return false;
		}
		return true;
	}
	
	/**
	 * 
	 * @param str
	 * @return true iff str is a legal movie (cinema) name
	 */
	public static boolean notAMovie(String str){
		return (str.contains( "(TV)" ) || str.contains( "(V)" ) || str.contains( "(VG)" )
		|| str.contains( "{" ) || str.startsWith( "\"" ) || str.contains( "????" ));
	}
	
	/**
	 * 
	 * @param genre
	 * @return the Genre genre represents and null in case it's a non valid genre. 
	 */
	public static Genre getGenre(String genre){
		if (genre.toLowerCase().equals("action"))
			return Genre.ACTION;
		if (genre.toLowerCase().equals("adventure"))
			return Genre.ADVENTURE;
		if (genre.toLowerCase().equals("adult"))
			return Genre.ADULT;
		if (genre.toLowerCase().equals("animation"))
			return Genre.ANIMATION;
		if (genre.toLowerCase().equals("biography"))
			return Genre.BIOGRAPHY;
		if (genre.toLowerCase().equals("comedy"))
			return Genre.COMEDY;
		if (genre.toLowerCase().equals("crime"))
			return Genre.CRIME;
		if (genre.toLowerCase().equals("documentary"))
			return Genre.DOCUMENTARY;
		if (genre.toLowerCase().equals("drama"))
			return Genre.DRAMA;
		if (genre.toLowerCase().equals("fantasy"))
			return Genre.FANTASY;
		if (genre.toLowerCase().equals("family"))
			return Genre.FAMILY;
		if (genre.toLowerCase().equals("film_noir"))
			return Genre.FILM_NOIR;
		if (genre.toLowerCase().equals("game_show"))
			return Genre.GAME_SHOW;
		if (genre.toLowerCase().equals("history"))
			return Genre.HISTORY;
		if (genre.toLowerCase().equals("horror"))
			return Genre.HORROR;
		if (genre.toLowerCase().equals("lifestyle"))
			return Genre.LIFESTYLE;
		if (genre.toLowerCase().equals("music"))
			return Genre.MUSIC;
		if (genre.toLowerCase().equals("musical"))
			return Genre.MUSICAL;
		if (genre.toLowerCase().equals("mystery"))
			return Genre.MYSTERY;
		if (genre.toLowerCase().equals("news"))
			return Genre.NEWS;
		if (genre.toLowerCase().equals("reality_tv"))
			return Genre.REALITY_TV;
		if (genre.toLowerCase().equals("romance"))
			return Genre.ROMANCE;
		if (genre.toLowerCase().equals("sci_fi"))
			return Genre.SCI_FI;
		if (genre.toLowerCase().equals("short"))
			return Genre.SHORT;
		if (genre.toLowerCase().equals("sport"))
			return Genre.SPORT;
		if (genre.toLowerCase().equals("talk_show"))
			return Genre.TALK_SHOW;
		if (genre.toLowerCase().equals("thriller"))
			return Genre.THRILLER;
		if (genre.toLowerCase().equals("war"))
			return Genre.WAR;
		if (genre.toLowerCase().equals("western"))
			return Genre.WESTERN;
		return null;
	}

	/**
	 * @param line
	 * @return classifies the reason for ratings into TEENS (PG & PG-13) and ADULS (R & NC-17)
	 * if doesn't match any of the classification- returns null.
	 */
	public static Rating getRating(String line) {
		if (line.toLowerCase().contains("rated pg-13"))
			return Rating.TEENS;
		if (line.toLowerCase().contains("rated r")||
				line.toLowerCase().contains("rated nc-17"))
			return Rating.ADULTS;
		return null;
	}
	/**
	 * @param str
	 * @return str with Apostrophes escaped: ***'*** -> ***''*** 
	 */
	
	public static String escapeApostrophe(String str){
		StringBuffer stb= new StringBuffer(str);
		int offset=0;
		int loc;
		//int len=stb.length();
		while (offset<stb.length() && (loc=stb.indexOf("'", offset))>=0){
			stb=stb.insert(loc, '\'');
			offset=loc+2;
		}
		return stb.toString();
	}
	
	/**
	 * @param year- valid year
	 * @return the relevant decade
	 */
	public static Decade getDecade(int year){
		if (year < 1980)
			return Decade.OLD;
		if (year < 1990)
			return Decade.MID;
		return Decade.NEW;
	}
	
	public static String turnToFemale(String name){
		return (name+" (F)");
	}
	
	public static String flipName(String name){
		int split=name.indexOf(',');
		if (split!=-1)
			return name.substring(split+1).trim()+" "+name.substring(0, split);
		return name;
	}
	
}
